% ECG_FILTER    Generalize function for ECG filters
%
% Composing
% 1. Notch filter
% 2. Bandpass filter
% 3. Smoothing filter
%
% Configuration
% Notch:        butterworth, adapative orders
% Bandpass:     Chebyshev type II design, adapative orders
% Smoothing:    Sgolay design, windows = 63, order = 8
%
% - with zero-pole phase shift adjustment
%   rotating the phase to original zero by function like
%   filtfilt and etc.
%
% See also:     ecg_bandpass, ecg_notch, ecg_smooth
% Modified at   27th Oct 2010
% Copyright:    Group 8
%------------------------------------------------------------------------
function [sgofilt,notchfilt,chebyfilt] = ecg_filter(X,Samp_freq)

if(nargin<2)
     Samp_freq = 553.625;
 end
 

%% Apply bandpass filter to the signal
    X_bandpass = ecg_bandpass(X);
    chebyfilt = X_bandpass;

    %% Apply notch filter to the signal
    X_notch = ecg_notch(X_bandpass);
    notchfilt = X_notch;
    
    %% Savitzky-Golay filtfilting
    %order: Wave preciseness
    %windows: Frame preciseness
    X_sgo = ecg_smooth(X_notch);
    sgofilt = X_sgo;

%Debug:
if(~nargout)
t = (0:length(X)-1)/Samp_freq;
subplot(4,1,1), plot(t,X,';Raw signal;'),xlim([30 35])
title('ECG filtering'),ylabel('Voltage / V')
subplot(4,1,2), plot(t,X_bandpass,';Stage 1 (Bandpass);'),xlim([30 35])
subplot(4,1,3), plot(t,X_notch,';Stage 2 (notch);'),xlim([30 35])
subplot(4,1,4), plot(t,X_sgo,';Stage 3 (Savitzky-Golay);'),xlim([30 35])
xlabel('time / s');
end

